<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="formData" :model="formData" label-width="120px" style="margin-left: 120px; margin-top:30px" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="formData.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submit">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 一般来说，组件引入注册之后，如果渲染都需要用组件名作为标签名，
              有一个例外，component 的 vue 内置标签可以渲染任意组件，被称作动态组件，只需要给它绑定一个 is 变量，传入需要渲染的组件名即可 -->
            <!-- <userInfo /> -->
            <component :is="userInfoName" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <component :is="jobInfoName" />
            <!-- <jobInfo /> -->
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { setEmployeeById } from '@/api/employess'
import userInfo from './components/userInfo.vue'
import jobInfo from './components/jobInfo.vue'
export default {
  components: {
    userInfo, jobInfo
  },
  data() {
    return {
      // 定义组件名作为值得变量
      // 传入 component 的 is 属性当中可以决定 component 显示的内容
      userInfoName: 'userInfo',
      jobInfoName: 'jobInfo',
      formData: {
        username: '',
        password2: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名称不能为空', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, message: '密码不能小于6位数', trigger: 'blur' },
          { max: 12, message: '密码不能大于12位数', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    // 获取个人信息
    async getUserDetailById() {
      this.formData = await getUserDetailById(this.$route.params.id)
      console.log(this.formData)
    },
    // 更改个人信息
    async submit() {
      // 校验表单
      await this.$refs.formData.validate()
      // 发起请求
      await setEmployeeById({ ...this.formData, password: this.formData.password2 })
      // 提示
      this.$message.success('修改成功')
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
